package com.example.zhaohang.utils;

import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by 赵航 on 2017/3/23.
 */

public class RotatePageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.75f;


    //手指滑动viewpager的时候，transformPage实时被调用
    public void transformPage(View view, float position) {

        //        view是viewpager的子控件
        //        view实际应该是2个页面在做动画，A页面，B页面
        //        A页面的position的取值范围[0,-1]
        //        B页面的position的取值范围[1,0]


        int pageWidth = view.getWidth();//控件宽320

        //根据position的取值范围，区分对应的页面对象，并给对应的页面对象设置动画

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);//将最左边的view设置为不可见

        } else if (position <= 0) { // [-1,0]
            // Use the default slide transition when moving to the left page
            //        A页面的position的取值范围[0,-1]

            view.setAlpha(1);
            view.setTranslationX(0);//a页面在手指移动时不移动（移动效果是由viewpager本身实现）
            view.setScaleX(1);
            view.setScaleY(1);

        } else if (position <= 1) { // (0,1]
            //        B页面的position的取值范围[1,0]
            // Fade the page out.
            //透明度的取值范围[0,1]
            view.setAlpha(1 - position);

            // Counteract the default slide transition
            //根据手指的滑动，动态设置b页面在x轴的移动
            //b页面在x轴移动的取值范围[-320,0]
            view.setTranslationX(pageWidth * -position);//当手指移动时，始终都保证b页面显示在屏幕正中间

            // Scale the page down (between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            //0.75+0.25*0 = 0.75
            //0.75+0.25*1 = 1
            //缩放取值范围：[0.75,1]
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);//将最右边的view设置为不可见
        }
    }
}
